package com.xiaohudie.SGGJUC;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/**
 * @Author xiao hu die
 * @Date 2022/3/8 13:22
 * @Version 1.0
 */
public class TestSemaphore {
 public static void main(String[] args) {
  Semaphore semaphore = new Semaphore(4);
  for (int i = 0; i <12 ; i++) {
   final int a =i;
   new Thread(()->{
    try {
     /**首先获得信号类*/
    semaphore.acquire();
    System.out.println(a+"进来了");
     TimeUnit.SECONDS.sleep(2);
     System.out.println(a+"离开了");
    } catch (InterruptedException e) {
     e.printStackTrace();
    }finally {
     /**把信号类释放*/
     semaphore.release();
    }
   },String.valueOf(i)).start();
  }
 }
}
